﻿using System;
using System.Net;
using System.Net.Sockets;

namespace BusStationClient
{
    public class UdpServer
    {
        ///// <summary>
        ///// 数据通讯的端口
        ///// </summary>
        //private readonly int _communicationPort;


        /// <summary>
        /// 负责接收客户端的数据
        /// </summary>
        private readonly UdpReceiveSocket _communicationRec;
        public event EventHandler<SocketAsyncEventArgs> OnReceivedData;


        /// <param name="dataComPort">数据通讯端口</param>
        public UdpServer(int dataComPort)
        {
            //_communicationPort = dataComPort;
            _communicationRec = new UdpReceiveSocket(dataComPort);
            _communicationRec.OnDataReceived += communicationRec_OnDataReceived;
     
        }


        public void Start()
        {
            //接收数据
            _communicationRec.StartReceive();
           // _communicationRec.isStop = false;

        }

        public void Stop()
        {
        //    _communicationRec.isStop = true;
        }

        #region 事件
        private void communicationRec_OnDataReceived(object sender, SocketAsyncEventArgs e)
        {
            if (OnReceivedData != null)
            {
                OnReceivedData(sender, e);
            }
        }

        #endregion

        public void SendData(byte[] data, IPEndPoint ipEndPoint)
        {
            _communicationRec.SendData(data, ipEndPoint);
        }

    }
}
